﻿#region Copyright (c) 11/27/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Windows;


namespace Amarok.Presentation
{
	/// <summary>
	/// This type implements the attached event ViewAction.
	/// </summary>
	public static class ViewActionManager
	{
		/// <summary>
		/// Identifies the ViewAction attached event.
		/// </summary>
		public static readonly RoutedEvent ViewActionEvent = EventManager.RegisterRoutedEvent(
			"ViewAction",
			RoutingStrategy.Bubble,
			typeof(ViewActionRoutedEventHandler),
			typeof(ViewActionManager));


		/// <summary>
		/// Adds a handler for the ViewAction attached event. 
		/// </summary>
		/// 
		/// <param name="element">
		/// The UIElement that listens to this event.</param>
		/// <param name="handler">
		/// The event handler.</param>
		public static void AddViewActionHandler(DependencyObject element, ViewActionRoutedEventHandler handler)
		{
			Verify.NotNull(element, "element");
			Verify.NotNull(handler, "handler");

			var uiElement = element as UIElement;

			if (uiElement != null)
				uiElement.AddHandler(ViewActionManager.ViewActionEvent, handler);
		}

		/// <summary>
		/// Removes a handler for the ViewAction attached event. 
		/// </summary>
		/// 
		/// <param name="element">
		/// The UIElement that listens to this event.</param>
		/// <param name="handler">
		/// The event handler.</param>
		public static void RemoveViewActionHandler(DependencyObject element, ViewActionRoutedEventHandler handler)
		{
			Verify.NotNull(element, "element");
			Verify.NotNull(handler, "handler");

			var uiElement = element as UIElement;

			if (uiElement != null)
				uiElement.RemoveHandler(ViewActionManager.ViewActionEvent, handler);
		}


		/// <summary>
		/// Raises the ViewAction attached event.
		/// </summary>
		/// 
		/// <param name="element">
		/// The UIElement on which to raise the routed event.</param>
		/// <param name="viewAction">
		/// The view action to associate with the routed event.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static void RaiseViewActionEvent(UIElement element, ViewAction viewAction)
		{
			Verify.NotNull(element, "element");
			Verify.NotNull(viewAction, "viewAction");

			element.RaiseEvent(new ViewActionRoutedEventArgs(
				ViewActionManager.ViewActionEvent,
				element,
				viewAction));
		}

	}
}
